package com.gxa.controller;
import com.gxa.common.R;
import com.gxa.pojo.Dept;
import com.gxa.pojo.Emp;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

@Controller
public class EmpController {
    /**
     * 如何编写CRUD
     */
     @RequestMapping(value = "/addEmp",method = RequestMethod.POST)
     public String addEmp(Integer empno,@RequestParam(value = "ename",required = false) String ename){//HTTP Status 400 – Bad Request
         System.out.println(empno);
         System.out.println(ename);//""  null
         return "";
     }

//    @RequestMapping(value = "/queryEmpList",method = RequestMethod.GET)
//    public String queryEmpList(@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "10") Integer pageSize){
//        System.out.println(currentPage);
//        System.out.println(pageSize);
//        return "";
//    }

     @RequestMapping(value = "/updateEmp",method = RequestMethod.POST)
     public String updateEmp(Emp emp,String name){
         System.out.println(name);
         System.out.println(emp);
         return "";
     }

    @RequestMapping(value = "/delEmp",method = RequestMethod.GET)
    public String delEmp(Integer[] empno){
        System.out.println(Arrays.toString(empno));
        return "";
    }

    @RequestMapping(value = "/queryEmp",method = RequestMethod.GET)
//    public String queryEmp(Emp emp,Dept dept){
//    public String queryEmp(Map map){//后期补充
    public String queryEmp(Emp emp){
        System.out.println(emp);
        System.out.println(emp.getDept());
        System.out.println(emp.getDeptList());
        return "";
    }

    @RequestMapping(value = "/queryEmpByRestful/{empno}/{ename}/{deptno}",method = RequestMethod.GET)
    public String queryEmpByRestful(@PathVariable("empno") Integer empno, @PathVariable("ename") String ename,@PathVariable("deptno") Integer deptno){
        System.out.println(empno);
        System.out.println(ename);
        System.out.println(deptno);
        return "";
    }


    /**
     * 响应：分为两类：
     *     1、跳转页面
     *        1.1 重定向：不带参数
     *            response.sendRedirect(url);
     *        1.2 转发：带参数
     *            request.getRequestDispatcher(url).forward(request,response);
     *        1.3 ModelAndView：带参数
     *        1.4 Model带参数：响应通过转发
     *     2、不跳转页面  response.setContentType("application/json;charset=utf-8")
     *        2.1 json 、text、html
     */
    @RequestMapping(value = "/queryEmpList",method = RequestMethod.GET)
    public String queryEmpList(Model model) throws IOException, ServletException {//一样的可以用Servlet API处理
        //重定向
//        response.sendRedirect("/success.jsp");
        //转发
//        request.setAttribute("username","gxa");
//        request.getRequestDispatcher("/success.jsp").forward(request,response);
        //返回json....
//        response.setContentType("application/json;charset=utf-8");
//        response.getWriter().write();//用fastjson转换成jsong格式，才能响应
//        response.getWriter().flush();
//        response.getWriter().close();
         model.addAttribute("name","liudehua");
         return "main";//默认是转发
//         return "forward:/success.jsp";//用关键字，那么不会走视图解析器
//         return "redirect:/success.jsp";//重定向
//        ModelAndView modelAndView =  new ModelAndView("main");
//        modelAndView.addObject("name","gxa");
//        return modelAndView;
    }


    @RequestMapping(value = "/findEmpList",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    @ResponseBody  //指定返回的是json数据+导入相关json依赖包
    public R findEmpList(Integer empno) throws IOException, ServletException {
        R r = new R();
        //调用Servcie..........
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(empno);
        return r;
    }


    @RequestMapping(value = "/main",method = RequestMethod.GET)
    @ModelAttribute("name") //name = gxagxa
    public String main(){
        return "gxagxa";
    }





}
